
package example

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/example"
    exampleReq "github.com/flipped-aurora/gin-vue-admin/server/model/example/request"
    "gorm.io/gorm"
)

type MediaFilesService struct {}
// CreateMediaFiles 创建mediaFiles表记录
// Author [yourname](https://github.com/yourname)
func (mediaFilesService *MediaFilesService) CreateMediaFiles(ctx context.Context, mediaFiles *example.MediaFiles) (err error) {
	err = global.GVA_DB.Create(mediaFiles).Error
	return err
}

// DeleteMediaFiles 删除mediaFiles表记录
// Author [yourname](https://github.com/yourname)
func (mediaFilesService *MediaFilesService)DeleteMediaFiles(ctx context.Context, ID string,userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.MediaFiles{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
              return err
        }
        if err = tx.Delete(&example.MediaFiles{},"id = ?",ID).Error; err != nil {
              return err
        }
        return nil
	})
	return err
}

// DeleteMediaFilesByIds 批量删除mediaFiles表记录
// Author [yourname](https://github.com/yourname)
func (mediaFilesService *MediaFilesService)DeleteMediaFilesByIds(ctx context.Context, IDs []string,deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
	    if err := tx.Model(&example.MediaFiles{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
            return err
        }
        if err := tx.Where("id in ?", IDs).Delete(&example.MediaFiles{}).Error; err != nil {
            return err
        }
        return nil
    })
	return err
}

// UpdateMediaFiles 更新mediaFiles表记录
// Author [yourname](https://github.com/yourname)
func (mediaFilesService *MediaFilesService)UpdateMediaFiles(ctx context.Context, mediaFiles example.MediaFiles) (err error) {
	err = global.GVA_DB.Model(&example.MediaFiles{}).Where("id = ?",mediaFiles.ID).Updates(&mediaFiles).Error
	return err
}

// GetMediaFiles 根据ID获取mediaFiles表记录
// Author [yourname](https://github.com/yourname)
func (mediaFilesService *MediaFilesService)GetMediaFiles(ctx context.Context, ID string) (mediaFiles example.MediaFiles, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&mediaFiles).Error
	return
}
// GetMediaFilesInfoList 分页获取mediaFiles表记录
// Author [yourname](https://github.com/yourname)
func (mediaFilesService *MediaFilesService)GetMediaFilesInfoList(ctx context.Context, info exampleReq.MediaFilesSearch) (list []example.MediaFiles, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&example.MediaFiles{})
    var mediaFiless []example.MediaFiles
    // 如果有条件搜索 下方会自动创建搜索语句
    if len(info.CreatedAtRange) == 2 {
     db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
    }
    
    if info.Size != nil {
        db = db.Where("size = ?", *info.Size)
    }
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }
        var OrderStr string
        orderMap := make(map[string]bool)
           orderMap["ID"] = true
           orderMap["CreatedAt"] = true
         	orderMap["size"] = true
       if orderMap[info.Sort] {
          OrderStr = info.Sort
          if info.Order == "descending" {
             OrderStr = OrderStr + " desc"
          }
          db = db.Order(OrderStr)
       }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&mediaFiless).Error
	return  mediaFiless, total, err
}
func (mediaFilesService *MediaFilesService)GetMediaFilesPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
